#include "mondialoguetransparence.h"

MonDialogueTransparence::MonDialogueTransparence(Calque * base, Calque * rajout, QWidget *parent) : QDialog(parent)
{
    this->setWindowTitle("OPACITE");

    this->maBase    = base;
    this->monRajout = rajout;

    this->label_img  = new QLabel(this);

    // definition de la transparence
    pourcentage   = new QLCDNumber(this);
    pourcentage->setSegmentStyle(QLCDNumber::Flat);
    pourcentage->move(50, 20);

    monSlider = new QSlider(Qt::Horizontal, this);
    monSlider->setRange(0, 100);
    monSlider->setGeometry(10, 60, 150, 20);
    monSlider->setValue(100);

    // appercu
    this->majTransparence(100);

    // label d'explication
    QLabel * label_explication  = new QLabel(this);
    QImage image_explication    = QImage(":new/Images/interrogation.png");
    image_explication   = image_explication.scaled(QSize(16, 16));

    QPixmap pixmap_explication  = QPixmap::fromImage(image_explication);
    label_explication->setPixmap(pixmap_explication);
    label_explication->move(150, 20);
    label_explication->setCursor(QCursor(Qt::PointingHandCursor));
    label_explication->setToolTip("Vous pourrez, apres avoir determine ce pourcentage d'opacite, positionner l'image inseree.");

    // bouton de validation
    QPushButton * valider   = new QPushButton("valider", this);
    valider->move(55, 90);

    // les signaux..
    QObject::connect(monSlider, SIGNAL(valueChanged(int)), this, SLOT(majTransparence(int)));
    QObject::connect(valider, SIGNAL(clicked()), this, SLOT(validation()));
}

MonDialogueTransparence::~MonDialogueTransparence(){
    delete label_img;
    delete pourcentage;
    delete monSlider;
}

void MonDialogueTransparence::majTransparence(int pourcentage){
    // modification de l'affichage digital
    this->pourcentage->display(pourcentage);

    // taille d'affichage maximale pour l'image de depart
    QSize size(100, 100);
    // on redimensionne l'image de depart
    QImage img1 = this->maBase->scaled(size, Qt::KeepAspectRatio);

    // calcul de la nouvelle taille de l'image rajoutee
    int ratio   = this->maBase->height()/img1.height();
    QSize size2(this->monRajout->width()/ratio, this->monRajout->height()/ratio);
    QImage img2 = this->monRajout->scaled(size2, Qt::KeepAspectRatio);

    QPainter painter(&img1);
    qreal opacite   = pourcentage/100.0;
    painter.setOpacity(opacite);
    painter.drawImage(0,0,img2);
    painter.end();

    QPixmap pixmap_img = QPixmap::fromImage(img1);
    label_img->setPixmap(pixmap_img);
    label_img->move(180, 10);
}

void MonDialogueTransparence::validation(){
    this->accept();
}

int MonDialogueTransparence::getValeurRetour(){
    return this->pourcentage->value();
}
